package com.depth.viewer.request.socket
{
	import com.depth.viewer.request.RequestBaseProxy;
	
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.events.TimerEvent;
	import flash.net.Socket;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	import flash.utils.setTimeout;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	
	public class SocketRquest extends RequestBaseProxy
	{
		public var socketUrl:String;
		public var socketPort:int;
		public var socket:Socket = new Socket();	 //定义Socket
		private var timer:Timer = new Timer(3000);
		
		private var timer1:Timer = new Timer(3600000);
		private var _actions:ArrayCollection = new ArrayCollection();
		
		private var _connectHandler:Function;
		public function set connectHandler(value:Function):void{
			this._connectHandler = value;
		}
		private var _resetSocketHandler:Function;
		public function set resetSocketHandler(value:Function):void{
			this._resetSocketHandler = value;
		}
		public function SocketRquest(socketUrl:String = null,socketPort:int = 0,reconnectDelay:Number = -1)
		{
			super(socketUrl);
			
			timer.addEventListener(TimerEvent.TIMER,function(e:TimerEvent):void{
				connect();
			});
			
			timer1.addEventListener(TimerEvent.TIMER,function(e:TimerEvent):void{
				if(socket.connected){
					socket.close();
					initSocket();
					
					if(_resetSocketHandler){
						_resetSocketHandler(socket);
					}
				}
			});
			
			if(reconnectDelay != -1){
				timer1.delay = reconnectDelay;
				timer1.start();
			}
			
			socketUrl = socketUrl;
			socketPort = socketPort;
			
			initSocket();
		}
		public function connect():void{
			socket.connect(socketUrl,socketPort);
		}
		private function initSocket():void{
			socket = new Socket();
			socket.addEventListener(Event.CONNECT, socketConnectHandler);
			socket.addEventListener(Event.CLOSE, socketCloseHandler);
			socket.addEventListener(ProgressEvent.SOCKET_DATA,socketLoadHandler);
			
			if(socketUrl && socketPort){
				connect();
			}
		}
		override public function request(methodName:String,... args:Array):void{
			if(!socket.connected){
				connect();
				_actions.addItem({methodName:methodName,params:args});
			}
			
			sendData(args[0]);
		}
		private function socketConnectHandler(e:Event):void{
			var actions:Array = _actions.toArray();
			while(actions.length > 0){
				var action:Object = actions.shift();
				//request(action.methodName,action.params);
				sendData(action.params[0]);
			}
			
			timer.stop();
			if(_connectHandler){
				_connectHandler(socket);
			}
			trace("成功建立socket连接");
		}
		private function socketCloseHandler(e:Event):void{
			if (_faultHandle)
			{
				_faultHandle("socket连接己断开");
			}
			
			trace("断开socket连接");
			setTimeout(function():void{
				timer.start();
				trace("正在重新连接");
			},1000);
		}
		private function sendData(data:Object):void{
			//var dataStr:String = JSON.encode(data);
			var dataBody:ByteArray = new ByteArray();	 //新建一个ByteArray存放数据
			
			//写入数据，writeUTFBytes方法，以utf-8格式传数据避免中文乱码
			
			// \r 表示：回车符(ACSII：13 或0x0d)，就是我们常说的硬回车。 \n 表示：换行(ACSII：10 或0x0a)，就是我们常说的软回车。
			
			dataBody.writeUTFBytes(data.toString());	 
			
			socket.writeBytes(dataBody);	 //写入Socket的缓冲区
			
			socket.flush();	 //调用flush方法发送信息
			trace(data.toString());
		}
		
		private function socketLoadHandler(e:ProgressEvent):void{	 //接到服务器信息
			if(_resultHandle){
				if(_data)
					_resultHandle(socket,_data);
				else
					_resultHandle(socket);
			}
		}
	}
}